<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>门禁</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">

    <script src="../../js/dashboard/jquery.min.js"></script>
    <script src="../../layui/layui.js"></script>

</head>

<body>
    <button type="button" class="layui-btn" onclick="addStrategy()">新增策略</button>
    <table id="strategy" lay-filter="policyTable"></table>


    <script>
        function loadTable(resData) {
            layui.use('table', function () {
                var table = layui.table;
                //第一个实例
                table.render({
                    elem: '#strategy',
                    height: 'full-60',
                    //data: resData,
                    url:"/strategy/queryStrategy",
                    parseData: function (res) { //res 即为原始返回的数据
                        if(typeof res.count === "undefined"){res.count=100}
                        return {
                            "code": 0, //解析接口状态
                            "count":res.count,
                            "data": res.data //解析数据列表
                        };
                    },
                    page:  { limit: 10 },//开启分页
                    cellMinWidth: 80,
                    cols: [[ //表头
                        { field: 'strategyName', title: '策略名称', sort: true, fixed: 'left' },
                        { field: 'strategyStatus', title: '类型' },
                        { field: 'weeks', title: '策略日期' },
                        { field: 'createTime', title: '策略时段', sort: true },
                        { fixed: 'right', title: '操作', align: 'center', toolbar: '#bar' }
                    ]]
                });
                //监听行工具事件
                table.on('tool(policyTable)', function (obj) {
                    var data = obj.data;
                    //console.log(obj)
                    if (obj.event === 'del') {
                        del(obj.data.strategyId);
                        console.log(obj.data,del);
                    } else if (obj.event === 'edit') {
                        layer.prompt({
                            formType: 2
                            , value: data.email
                        }, function (value, index) {
                            obj.update({
                                email: value
                            });
                            layer.close(index);
                        });
                    }
                });

            });

        }
        function addStrategy() {
            layer.open({
                type: 2,
                content: './addStrategy.html',
                area: ['800px', '600px'],
                end:function (){
                    loadTable();
                }
            });
        }
        function del(id) {
            $.ajax({
                url: "/strategy/delete/"+id,
                type: "delete",
                dataType: 'json',
                async: true, //是否异步请求
                success: function (res) {
                    alert("删除成功");
                    loadTable();
                },
                error: function (err){
                    console.log(err)
                }
            });
        }
        loadTable();
    </script>

    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
        <a class="layui-btn layui-btn-xs" lay-event="del"  >删除</a>

    </script>
</body>
</html>